﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Xml.XPath;

namespace workshop3
{
    public partial class Formrubric : Form
    {
        DataTable rubric = new DataTable();
        DataColumn itemid = new DataColumn("itemid");
        DataColumn itemname = new DataColumn("itemname");
        DataColumn itempercentage = new DataColumn("itempercentage");
        string rubricid;
        string courseid;
        List<string> yearlist = new List<string>();
        List<string> semelist = new List<string>();
       DataTable courselist = new DataTable();
        DataColumn id = new DataColumn("courseid");
        DataColumn coursename = new DataColumn("coursename");
        public Formrubric()
        {
            InitializeComponent();
        }

        private void Formrubric_Load(object sender, EventArgs e)
        {
            rubric.Columns.Add(itemid);
            rubric.Columns.Add(itemname);
            rubric.Columns.Add(itempercentage);
            //MessageBox.Show("enter");
            if (File.Exists("rubric.xml")&&File.Exists("course.xml"))
            {
                
                //XmlDocument rubdoc = new XmlDocument();
                XmlDocument coursedoc = new XmlDocument();
                coursedoc.Load("course.xml");
                //rubdoc.Load("rubric.xml");
                string coursepath = "//course";
                //string rubpath = "//rubrics/rubric";
                XmlNodeList coursenodelist = coursedoc.SelectNodes(coursepath);
                //XmlNodeList rubnodelist = rubdoc.SelectNodes(rubpath);
                //courselist.Columns.Add(id);
                //courselist.Columns.Add(coursename);
                if (coursenodelist!=null)
                {
                    for(int i = 0; i < coursenodelist.Count; i++)
                    {
                        if (yearlist.Count == 0 && semelist.Count == 0)
                        {
                            //courselist.Rows.Add(coursenodelist[i].Attributes[0].Value.ToString(),coursenodelist[i].ChildNodes[0].InnerText);
                            yearlist.Add(coursenodelist[i].ChildNodes[1].InnerText);
                            semelist.Add(coursenodelist[i].ChildNodes[2].InnerText);
                        }
                        else
                        {
                            if (yearlist.Contains(coursenodelist[i].ChildNodes[1].InnerText))
                            {
                                //continue;
                            }
                            else
                            {
                                yearlist.Add(coursenodelist[i].ChildNodes[1].InnerText);
                            }
                            if (semelist.Contains(coursenodelist[i].ChildNodes[2].InnerText))
                            {
                                //continue;
                            }
                            else
                            {
                                semelist.Add(coursenodelist[i].ChildNodes[2].InnerText);
                            }

                            //courselist.Rows.Add(coursenodelist[i].Attributes[0].Value.ToString(),coursenodelist[i].ChildNodes[0].InnerText);
                            
                        }
                    }
                    cbyear.SelectedValueChanged -= cbyear_SelectedValueChanged;
                    cbsemester.SelectedValueChanged -= cbsemester_SelectedValueChanged;
                    cbcourse.SelectedValueChanged -= cbcourse_SelectedValueChanged;
                    cbrubric.SelectedValueChanged -= cbrubric_SelectedValueChanged;

                    cbyear.DataSource = yearlist;
                    cbsemester.DataSource = semelist;
                    //cbcourse.DataSource = courselist;
                    //cbcourse.DisplayMember = courselist.Columns[1].ToString() ;
                    //cbcourse.ValueMember = courselist.Columns[0].ToString();

                    cbyear.SelectedValueChanged += cbyear_SelectedValueChanged;
                    cbsemester.SelectedValueChanged += cbsemester_SelectedValueChanged;
                    //cbcourse.SelectedValueChanged -= cbcourse_SelectedValueChanged;
                }
                else
                {
                    MessageBox.Show("file empty!");
                }
            }
            else
            {
                MessageBox.Show("file not exist!");
            }
        }

        private void cbyear_SelectedValueChanged(object sender, EventArgs e)
        {
            cbrubric.DataSource = null;
            dgvrubric.DataSource = null;
            cbcourse.DataSource = null;
            lblname.Text = string.Empty;
            ChangeCourse(cbyear.SelectedValue.ToString(), cbsemester.SelectedValue.ToString());
        }

        private void cbsemester_SelectedValueChanged(object sender, EventArgs e)
        {
            cbrubric.DataSource = null;
            dgvrubric.DataSource = null;
            cbcourse.DataSource = null;
            lblname.Text = string.Empty;
            ChangeCourse(cbyear.SelectedValue.ToString(), cbsemester.SelectedValue.ToString());
        }

        private void cbcourse_SelectedValueChanged(object sender, EventArgs e)
        {
            cbrubric.DataSource = null;
            dgvrubric.DataSource = null;
            lblname.Text = string.Empty;
            if (cbcourse.DataSource != null)
            {
                ChangeRubric(cbcourse.SelectedValue.ToString());
            }
        }

        private void ShowRubric(string rubricid)//dynamic show the rubric 
        {
            bool find = false;
            rubric.Clear();
            if (File.Exists("rubric.xml"))
            {
                XmlDocument rubdoc = new XmlDocument();
                rubdoc.Load("rubric.xml");
                string rubpath = "//rubrics/rubric";
                XmlNodeList rubnodelist = rubdoc.SelectNodes(rubpath);
                if (rubnodelist.Count != 0)
                {
                    for(int i= 0; i < rubnodelist.Count; i++)
                    {
                        if (rubnodelist[i].Attributes[0].Value == rubricid)
                        {
                            find = true;
                           lblname.Text= rubnodelist[i].Attributes[1].Value;
                            XmlNodeList item = rubnodelist[i].ChildNodes;
                            foreach(XmlNode xn in item)
                            {
                                rubric.Rows.Add(xn.Attributes[0].Value.ToString(), xn.Attributes[1].Value.ToString(), xn.InnerText);
                            }
                        }
                    }
                    if (find)
                    {
                        dgvrubric.DataSource = rubric;
                        dgvrubric.Columns[0].Visible = false;
                    }
                }
                else
                {
                    dgvrubric.DataSource = null;
                }

            }
        }

        private void ChangeCourse(string year, string semester)//dynamic show the course name in the combobox
        {
            bool find = false;
            DataTable dt0 = new DataTable();
            DataColumn dc3 = new DataColumn("coursename");
            DataColumn dc4 = new DataColumn("courseid");
            dt0.Columns.Add(dc4);
            dt0.Columns.Add(dc3);
            if (File.Exists("course.xml"))
            {
                XmlDocument doc = new XmlDocument();
                doc.Load("course.xml");
                string path = "//course";
                XmlNodeList nodelist = doc.SelectNodes(path);
                if (nodelist.Count != 0)
                {
                    for (int i = 0; i < nodelist.Count; i++)
                    {
                        if (nodelist[i].ChildNodes[1].InnerText == year &&nodelist[i].ChildNodes[2].InnerText == semester)
                        {
                            find = true;
                            dt0.Rows.Add(nodelist[i].Attributes[0].Value, nodelist[i].ChildNodes[0].InnerText);
                        }

                    }
                    if (find)//if find the course 
                    {
                        cbcourse.DataSource = dt0;
                        cbcourse.DisplayMember = dt0.Columns[1].ToString();
                        cbcourse.ValueMember = dt0.Columns[0].ToString();
                        cbcourse.SelectedValueChanged += cbcourse_SelectedValueChanged;
                    }
                    
                   
                }
                else
                {
                    MessageBox.Show("Can not find any course!");
                }
                
                //return dt0;

            }
            //return null;
        }

        private void ChangeRubric (string courseid)//dynamic load the rubric data into combobox
        {
            bool find = false;
            DataTable dt0 = new DataTable();
            DataColumn dc3 = new DataColumn("rubricname");
            DataColumn dc4 = new DataColumn("rubricid");
            dt0.Columns.Add(dc4);
            dt0.Columns.Add(dc3);
            if (File.Exists("rubric.xml"))
            {
                XmlDocument doc = new XmlDocument();
                doc.Load("rubric.xml");
                string path = "//rubric";
                XmlNodeList nodelist = doc.SelectNodes(path);
                if (nodelist.Count != 0)
                {
                    for (int i = 0; i < nodelist.Count; i++)
                    {
                        if (nodelist[i].Attributes[2].Value==courseid)
                        {
                            find = true;
                            dt0.Rows.Add(nodelist[i].Attributes[0].Value, nodelist[i].Attributes[1].Value);
                        }

                    }
                   if(find){
                        cbrubric.DataSource = dt0;
                        cbrubric.DisplayMember = dt0.Columns[1].ToString();
                        cbrubric.ValueMember = dt0.Columns[0].ToString();
                        cbrubric.SelectedValueChanged += cbrubric_SelectedValueChanged;
                    }


                }
                else
                {
                    MessageBox.Show("Can not find any rubric!");
                }

                //return dt0;

            }
            //return null;
        }

        private void cbrubric_SelectedValueChanged(object sender, EventArgs e)
        {
            lblname.Text = string.Empty;
            dgvrubric.DataSource = null;
            if (cbrubric.DataSource != null)
            {
                ShowRubric(cbrubric.SelectedValue.ToString());
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //Formaddrubric addrubric = new Formaddrubric();
            //addrubric.Owner = this;
            //addrubric.ShowDialog();
        }

        private void btnaddrubric_Click(object sender, EventArgs e)
        {
            Formaddrubric addrubric = new Formaddrubric(yearlist, semelist);
            addrubric.Owner = this;
            addrubric.ShowDialog();
        }

        private void btnedit_Click(object sender, EventArgs e)
        {
            DataTable tranfer = new DataTable();//create a new datatable used to tranfer the selected rubric to the edit page
            tranfer = rubric.Copy();//copy the datatable to the new datatable just created
            tranfer.Columns.Remove(tranfer.Columns[0]);//remove the unused column 
            if (rubric.Rows.Count > 0)
            {
                //rubric.Clear();
                ClassCreateXML.deletexml("rubric.xml", "//rubric", cbrubric.SelectedValue.ToString());//first delete the existing rubric 
                Formeditrubric edit = new Formeditrubric(tranfer, yearlist, semelist,cbcourse.SelectedValue.ToString(),lblname.Text);
                edit.Owner = this;
                edit.ShowDialog();
                rubric.Clear();//prevent the binded changes between the two datatable in two page
            }
            else
            {
                MessageBox.Show("Please select one rubric first!");
            }
        }

        private void btnsave_Click(object sender, EventArgs e)
        {
            
        }

        private void btn_user_Click(object sender, EventArgs e)
        {
            Formmanagement back = new Formmanagement();
            back.Show();
            this.Hide();
        }
    }
}
